import dt from '@dt/dt'
import productOpt from '@/biz/option/product.js'
import ruleOpt from '@/biz/option/rule.js'
import levelMgr from '@/biz/bonus/level.js'

function target(_this, flag) {
  let targetValues = dt.dictInfo().TargetValue.filter(i => i.key == '1')
  if (_this.info.targetType == 'MONTH_PERSON') {
    targetValues = dt.dictInfo().TargetValue
  }
  return [{
    title: '跳点指标',
    group: [{
      key: 'targetType',
      col: 6,
      disabled: flag != 'detail' ? false : true,
      select: {
        options: dt.dictInfo().JumpTarget
      },
      change: () => {
        _this.update()
        _this.updateColumn()
      }
    }, {
      key: 'targetValue',
      col: 4,
      disabled: flag != 'detail' ? false : true,
      select: {
        options: targetValues
      },
      change: () => {
        _this.updateColumn()
      }
    }, {
      col: 6,
      disabled: flag != 'detail' ? false : true,
      button: {
        title: '前往指标设置',
        type: 'primary',
        click: () => {
          _this.$Modal.confirm({
            title: '<p>未保存的数据将会丢失，确定要离开此页吗？</p>',
            okText: '离开此页',
            cancelText: '留在此页',
            width: '450',
            onOk: () => {
              _this.$router.push({
                name: 'buildingConfigure',
                params: {
                  fromCustomer: 'targetManagement',
                },
              })
            },
            onCancel: () => {}
          })
        }
      }
    }],
    rules: [
      ruleOpt.required('请选择跳点指标')
    ]
  }]
}

function productInfo(_this, flag) {
  return [productOpt.create({
    key: 'productIds',
    multiple: true,
    type: 'array',
    disabled: flag != 'detail' ? false : true,
    hid: _this.$route.query.housesId || dt.buildInfo().housesId,
  })]
}

function effectiveDate(flag) {
  return [{
    title: '生效日期',
    placeholder: '请选择生效日期',
    key: 'effectiveStartDate',
    disabled: flag != 'detail' ? false : true,
    date: {
      type: 'daterange'
    },
    data: {
      from: (info) => {
        if (info.effectiveStartDate && info.effectiveEndDate) {
          return [info.effectiveStartDate, info.effectiveEndDate]
        }
        return null
      },
      to: (v) => {
        return {
          effectiveStartDate: v[0],
          effectiveEndDate: v[1],
        }
      }
    },
    rules: [
      ruleOpt.required('请选择生效日期')
    ]
  }]
}

function rankRange(_this, flag) {
  if (_this.info.targetValue == '2') {
    return {
      edit: {
        key: 'startValue',
        disabled: flag != 'detail' ? false : true,
        input: {
          type: 'number',
          min: 0
        }
      }
    }
  } else {
    return {
      group: [{
        key: 'startValue',
        col: 5,
        disabled: flag != 'detail' ? false : true,
        input: {
          type: 'number',
          min: 0
        }
      }, {
        key: 'startInterval',
        col: 4,
        disabled: flag != 'detail' ? false : true,
        select: {
          options: dt.dictInfo().Interval
        }
      }, {
        placeholder: dt.dictInfo().__value('JumpTarget', _this.info.targetType),
        col: _this.info.targetType == 'MONTH_PERSON' || _this.info.targetType == 'MONTH_HOUSES' || _this.info.targetType == 'QUARTER_HOUSES' ? 5 : 7,
        text: {}
      }, {
        key: 'endInterval',
        col: 4,
        disabled: flag != 'detail' ? false : true,
        select: {
          options: dt.dictInfo().Interval
        }
      }, {
        key: 'endValue',
        col: 4,
        disabled: flag != 'detail' ? false : true,
        input: {
          type: 'number',
          min: 0
        }
      }]
    }
  }
}

function handles(flag) {
  return flag != 'detail' ? [{
      field: 'handles',
      title: '操作',
      width: 60,
      cell: (row) => {
        return {
          btn: [{
            title: '删除',
            event: 'onDelete'
          }]
        }
      }
    }] : []
}

function options(_this, flag) {
  return [
    [{
      title: '跳点方案名称',
      placeholder: '请输入跳点方案名称',
      key: 'jumpName',
      disabled: flag != 'detail' ? false : true,
      input: {},
      rules: [
        ruleOpt.required('请输入跳点方案名称')
      ]
    }],
    [{
      title: '跳点取值',
      placeholder: '请选择跳点取值',
      key: 'jumpType',
      disabled: flag != 'detail' ? false : true,
      select: {
        options: dt.dictInfo().JumpPointType
      },
      rules: [
        ruleOpt.required('请选择跳点取值')
      ]
    }],
    target(_this, flag),
    [{
      title: '基础提点',
      placeholder: '请选择基础提点',
      key: 'baseMentionSomething',
      disabled: flag != 'detail' ? false : true,
      select: {
        options: dt.dictInfo().baseMentionSomething
      },
      rules: [
        ruleOpt.required('请选择基础提点')
      ]
    }],
    [{
      title: '涉及岗位',
      placeholder: '请选择岗位',
      key: 'postIds',
      disabled: flag != 'detail' ? false : true,
      select: {
        multiple: true,
        options: levelMgr.positions()
      },
      data: {
        to: (v) => {
          let postIds = [], postNames = []
          for (let item of v) {
            postIds.push(item.value)
            postNames.push(item.label)
          }
          return {
            postIds,
            postNames: postNames.join(',')
          }
        }
      },
      rules: [
        ruleOpt.required('请选择岗位', 'array')
      ]
    }],
    productInfo(_this, flag),
    [{
      title: '生效方式',
      key: 'effectiveWay',
      disabled: flag != 'detail' ? false : true,
      radio: {
        options: dt.dictInfo().effectiveWay.map(i => ({ ...i, disabled: flag != 'detail' ? false : true }))
      },
      rules: [
        ruleOpt.required('请选择生效方式')
      ]
    }],
    effectiveDate(flag),
  ]
}

function column(_this, flag) {
  return [
    {
      type: 'index',
      title: '序号',
      width: 60
    },
    {
      title: _this.info.targetValue == '2' ? '排名' : (_this.info.targetType == 'MONTH_PERSON' || _this.info.targetType == 'MONTH_HOUSES' || _this.info.targetType == 'QUARTER_HOUSES' ? '区间(万元)' : '区间(%)'),
      cell: (row) => {
        return rankRange(_this, flag)
      }
    },
    {
      title: '提点设置',
      width: _this.info.targetValue == '2' ? '' : 260,
      cell: (row) => {
        return {
          group: [{
            key: 'mentionSomethingType',
            col: 10,
            disabled: flag != 'detail' ? false : true,
            select: {
              options: dt.dictInfo().mentionSomethingType
            },
            change: () => {
              row.mentionSomethingPercentage = row.mentionSomethingType == '1' ? 100 : row.mentionSomethingPercentage
            }
          }, {
            key: 'mentionSomethingPercentage',
            col: 9,
            disabled: flag != 'detail' ? false : true,
            input: {
              type: 'number',
              min: 0
            }
          }, {
            placeholder: row.mentionSomethingType == '1' ? '%' : row.mentionSomethingType == '2' ? '万分之' : '元',
            col: 5,
            text: {}
          }],
        }
      }
    },
    ...handles(flag)
  ]
}

export default {
  options,
  column
}
